package com.shop.controller;

import com.alibaba.fastjson.JSONObject;
import com.shop.dao.BizUserDao;
import com.shop.dao.impl.BizUserDaoImpl;
import com.shop.entity.Store;
import com.shop.entity.User;
import com.shop.exception.*;
import com.shop.service.BizUservice;
import com.shop.service.impl.BizServiceimpl;
import com.shop.util.GetMap;
import com.shop.util.MD5Util;
import com.shop.util.PrintJson;
import com.sun.net.httpserver.HttpContext;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import org.omg.Messaging.SYNC_WITH_TRANSPORT;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.Executor;

//http://localhost:8088/projectname/store/?????????
@WebServlet("/store/*")
public class BizController extends HttpServlet {
    BizUservice b = new BizServiceimpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String uri = req.getRequestURI();
        System.out.println(uri);
        if ("/shop/store/register".equals(uri)) {
            register(req, resp);
     } if ("/shop/store/update".equals(uri)){
            update(req,resp);
        }
        if("/shop/store/select".equals(uri)){
            select(req,resp);
        }
        if ("/shop/store/againupdate".equals(uri)) {
            againupdate(req,resp);
            System.out.println(1);
        }

    }
    //修改商家名
    private void againupdate(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("againupdate function");
        JSONObject json = PrintJson.paraseJson(req);
        int s_id =Integer.parseInt(json.getString("s_id"));
        System.out.println("s_id"+s_id);
        Map<String,Object> map = null;
        String s_storename=json.getString("s_storename");
        System.out.println("s_storename"+s_storename);

        Store store1 =b.againupdate(s_id,s_storename);
        map = GetMap.getMap(200, "插入成功");
        map.put("data", store1);
        System.out.println(store1.getS_id());
        PrintJson.printJsonObj(resp, map);
    }

    private void select(HttpServletRequest req, HttpServletResponse resp) {
        int u_id =Integer.parseInt(req.getParameter("u_id"));
        System.out.println("u_id"+u_id+"select Function");
        Map<String, Object> map = null;

        try {
            Store store =b.selectStore( u_id);
            map = GetMap.getMap(200, "插入成功");
            map.put("data", store);


        } catch (ErrorSidException e) {
            map = GetMap.getMap(400, "插入失");
            map.put("data", " ");

            e.printStackTrace();
        }
        PrintJson.printJsonObj(resp, map);
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("是否调用updatafunction");
       // JSONObject json = PrintJson.paraseJson(req);
        int  s_id =Integer.parseInt(req.getParameter("s_id"));
        System.out.println("打印s_id"+s_id);
        String s_name =req.getParameter("s_id");
        String s_pwd =req.getParameter("s_pwd");
        String s_storename=req.getParameter("s_storename");
        Store store =new Store(s_id,s_name,s_pwd,s_storename);
        System.out.println(s_id);
        System.out.println(s_name);
        System.out.println(s_pwd);
        System.out.println(s_storename);

        Map<String, Object> map = null;

        try {
            Store store1 =b.update(store);
            map = GetMap.getMap(200, "插入成功");
            map.put("data", store1);
            } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (NDUException e) {
            e.printStackTrace();
            map = GetMap.getMap(400, "请输入正确的商家id");
            map.put("data", "");


        }
        PrintJson.printJsonObj(resp, map);
    }


    private void register(HttpServletRequest req, HttpServletResponse resp)  {
        System.out.println("席新家在插入");
        Store store = new Store();
//        String name = req.getParameter("s_name");
//        System.out.println("Biz：" + name);
//        String pwd = req.getParameter("s_pwd");
//        System.out.println("pwd：" + pwd);
//
//        String storename = req.getParameter("s_storename");
//        int uid = Integer.parseInt(req.getParameter("u_id"));
          JSONObject json = PrintJson.paraseJson(req);
          String s_name = json.getString("s_name");
          String s_pwd =json.getString("s_pwd");
          String s_storename=json.getString("s_storename");
          int u_id =json.getInteger("u_id");

        store.setS_name(s_name);
        store.setS_pwd(s_pwd);
        store.setS_storename(s_storename);
        store.setU_id(u_id);
        System.out.println("U_id"+u_id);
        Map<String, Object> map = null;
        System.out.println("json"+store.getS_name()+"json"+store.getS_storename());
        try {
            Store store1 = b.register(store);
            map = GetMap.getMap(200, "插入成功");
            map.put("data", store1);


        }  catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (useIsNoException e) {
            map = GetMap.getMap(400, "插入失败");
            map.put("data", " ");

            e.printStackTrace();
        }
        PrintJson.printJsonObj(resp, map);

    }
}        //

